package com.baiytfp.hf.HFDataExchange.common.base;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_info;
import com.baiytfp.hf.baiytfp.framework.shiro.ShiroUtil;
import com.baiytfp.hf.baiytfp.hfutils.*;
import com.baiytfp.hf.HFDataExchange.common.base.anotation.Record;
import com.baiytfp.hf.HFDataExchange.common.utils.CommonUtil;
import com.baiytfp.hf.HFDataExchange.common.utils.FtlUtil;
import com.baiytfp.hf.HFDataExchange.common.utils.PathInfo;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Steven-HF on 2017/5/10.
 */
public class ControllerDataInterceptor extends HandlerInterceptorAdapter {

    // Controller 前执
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        if (handler != null && handler instanceof HandlerMethod ) {
            // 为Control中的service设置当前登入的用
            HandlerMethod handlermethod = (HandlerMethod) handler;
            //Object beanObj = handlermethod.getBean();
            Record record = handlermethod.getMethodAnnotation(Record.class);
        	if(record != null) {
            	//new HFLog(HFLog.TYPE_OPERATION).logRequest(request.getRequestURI(),CommonUtil.paseJson(request));
            	new HFLog(HFLog.TYPE_OPERATION).logRequest(record.desc(),request);
        	}
        	
        	
        }
        return true;
    }

    // 运行Controller后运
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // 获取ftl模板前缀，并存放在session
        if (modelAndView != null) {
            //将枚举载入ftl中， 直接用枚举名getValue("key") 前端调用
            FtlUtil.setDefaultStaticModel(modelAndView);

            // 添加通用参数到ftl模板
            modelAndView.addObject("path", PathInfo.getAppPath(request));
            modelAndView.addObject("ftl_skin", HFConfig.getIstance().getPropConfig("ftl_templatePath", "default"));

            //用户信息
            
            String usercode = HFWeb.getIstance().getUserCode();
            if(!CommonUtil.isEmpty(usercode)) {
            	  modelAndView.addObject("usercode", usercode);
                  User_info userInfoVO = HFDBHelper.read(TableName.USER_INFO, " where user_code=? ", User_info.class, usercode);
                  if(userInfoVO != null){
                      modelAndView.addObject("user", userInfoVO);
                  }
            }
          

            modelAndView.addObject("hf_version", "20181107");

            //去掉 view/
            String viewName = PathInfo.getTemplatePath(request) + modelAndView.getViewName().replace("view/", "");

            // 添加模板前缀
            modelAndView.setViewName(viewName);
        }
    }
}
